<?php
/**
 * class.tree.php           树类库
 *
 * @author                  mashanling(msl-138@163.com)
 * @date                    2011-12-20 16:03:08
 * @last modify             2012-07-19 11:17:12 by mashanling
 */

class Tree {
    static $tree = array();

    /**
     * 将数组转化为树数组
     *
     * @param array  $array    待转化数组
     * @param string $pk       主键，默认id
     * @param string $pid      父id字段，默认parent_id
     * @param string $child    子类键名，默认children
     *
     * @return array 转化后的树数组
     */

    static function array2tree($array = array(), $pk = 'id', $pid = 'parent_id', $child = 'children') {
        $tree = array();

        if (is_array($array)) {
            $refer = array();//创建基于主键的数组引用

            foreach ($array as $key => $data) {
                $_key = is_object($data) ? $data->$pk : $data[$pk];

                if (!isset($array[$key]['leaf'])) {//支持手工控制leaf by mashanling on 2012-07-19 09:04:17
                    $array[$key]['leaf'] = true;
                }

                if (isset($array[$key]['checked']) && !is_bool($array[$key]['checked'])) {//支持手工控制checked by mashanling on 2012-07-19 09:05:33
                    unset($array[$key]['checked']);
                }
                elseif (!isset($array[$key]['checked'])) {
                    $array[$key]['checked'] = false;
                }

                $refer[$_key] = &$array[$key];
            }

            foreach ($array as $key => $data) {
                $parent_id = is_object($data) ? $data->$pid : $data[$pid];

                if ($parent_id && isset($refer[$parent_id])) {//存在parent 增加isset判断 by mashanling on 2012-07-19 11:16:52
                    $refer[$parent_id]['leaf'] = false;
                    $parent = &$refer[$parent_id];
                    $parent[$child][] = &$array[$key];
                }
                else {
                    $tree[] = &$array[$key];
                }

                unset($array[$key]);
            }
        }

        return $tree;
    }//end array2tree

    /**
     * 将树数组转化为数组
     *
     * @param array  $tree     待转化树数组
     * @param int    $level    层次，默认0
     * @param string $item_key 缩进字段，默认false
     * @param string $child    子类标识，默认children
     *
     * @return array 数组
     */
    static function tree2array($tree, $level = 0, $item_key = false, $child = 'children') {
        $xu = 0;

        foreach ($tree as $key => $val) {

            if ($item_key) {
                $temp_str = ' ';

                if ($level > 0) {

                    for ($xu = 1; $xu < $level; $xu++) {
                        $temp_str .= '&nbsp;&nbsp;│';
                    }

                    $temp_str .= '&nbsp;&nbsp;├ ';
                }

                $val[$item_key] = $temp_str . $val[$item_key];
            }

            if (array_key_exists($child, $val)) {
                $temp_arr = $val[$child];

                unset($val[$child]);

                self::$tree[$key] = $val;
                self::tree2array($temp_arr, $level + 1, $item_key, $child);
            }
            else {
                self::$tree[$key] = $val;
            }
        }
    }//end tree2array
}
?>